package com.test.gpa.test.suanfa;

import java.util.*;

 class ListNode {
    int val;
    ListNode next;

    ListNode() {
    }

    ListNode(int val) {
        this.val = val;
    }

    ListNode(int val, ListNode next) {
        this.val = val;
        this.next = next;
    }
}

public class Solution {
    public static void main(String[] args) {
        ListNode l1 = new ListNode(0);
        ListNode listNode = new ListNode(3);
        ListNode listNode1 = new ListNode(5);
        l1.next = listNode;
        listNode.next = listNode1;

        ListNode l2 = new ListNode(6);
        ListNode listNode2 = new ListNode(4);
        ListNode listNode3 = new ListNode(8);
        l2.next = listNode2;
        listNode2.next = listNode3;
        ListNode head = addTwoNumbers(l1, l2);
        while (head != null) {
            System.out.println(head.val);
            head = head.next;
        }

    }

    public static ListNode addTwoNumbers1(ListNode l1, ListNode l2) {
        String s1 = "";
        String s2 = "";
        while (l1 != null) {
            s1 = "" + l1.val + s1;
            l1 = l1.next;
        }
        while (l2 != null) {
            s2 = "" + l2.val + s2;
            l2 = l2.next;
        }
        String s = String.valueOf(Long.valueOf(s1) + Long.valueOf(s2));


        Deque<Integer> deque = new ArrayDeque();
        for (int i = 0; i < s.length(); i++) {
            int c = Integer.valueOf(s.substring(i, i + 1));
            deque.addLast(c);
        }
        ListNode listNode = new ListNode(deque.removeLast());
        ListNode head = listNode;
        while (!deque.isEmpty()) {
            listNode.next = new ListNode(deque.removeLast());
            listNode = listNode.next;
        }
        return head;
    }


    public static ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode head = null, tail = null;
        int carry = 0;
        while (l1 != null || l2 != null) {
            int n1 = l1 != null ? l1.val : 0;
            int n2 = l2 != null ? l2.val : 0;
            int sum = n1 + n2 + carry;
            if (head == null) {
                head = tail = new ListNode(sum % 10);
            } else {
                tail.next = new ListNode(sum % 10);
                tail = tail.next;
            }
            carry = sum / 10;
            if (l1 != null) {
                l1 = l1.next;
            }
            if (l2 != null) {
                l2 = l2.next;
            }
        }
        if (carry > 0) {
            tail.next = new ListNode(carry);
        }
        return head;
    }

    public List<Integer> diffWaysToCompute(String expression) {
        if (expression == null || expression.length() == 0) {
            return new ArrayList<>();
        }
        List ans = new ArrayList();
        for (int i = 0; i < expression.length(); i++) {
            if (!Character.isDigit(expression.charAt(i))) {
                List<Integer> left = diffWaysToCompute(expression.substring(0, i));
                List<Integer> right = diffWaysToCompute(expression.substring(i+1));
                for (Integer left1 :
                        left) {
                    for (Integer right1 :
                            right) {
                        if(expression.charAt(i)=='+'){
                            ans.add(left1+right1);
                        }else if (expression.charAt(i)=='-'){
                            ans.add(left1-right1);
                        }else ans.add(left1*right1);
                    }
                }
            }
        }

      if(ans.isEmpty()){

         ans.add(Integer.valueOf(expression));
      }
      return ans;
    }

}
